import { getUserDetail, getUserProfile, login } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    updateUserInfo(state, newInfo) {
      state.userInfo = newInfo
    }
  },
  actions: {
    async login(context, payload) {
      const res = await login(payload)
      console.log(res)
      context.commit('updateToken', res.data)
    },
    // getInfo() {},
    async getUserInfo(context) {
      const res = await getUserProfile()
      console.log(res)
      const data = await getUserDetail(res.data.userId)
      console.log(data)
      context.commit('updateUserInfo', { ...res.data, ...data.data })
    },
    logout(context) {
      context.commit('updateToken', '')
      context.commit('updateUserInfo', {})
    }
  }
}

